package ms.jasim.framework;

import java.io.Serializable;

import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;

import ms.utils.NamedList;

public class NamedListComboBoxModel<E extends Object> extends AbstractListModel implements MutableComboBoxModel, Serializable {
	private static final long serialVersionUID = 1L;
	
	NamedList<E> objects;
	Object selectedObject;

	/**
	 * Constructs a DefaultComboBoxModel object initialized with a vector.
	 * 
	 * @param v
	 *            a Vector object ...
	 */
	public NamedListComboBoxModel(NamedList<E> v) {
		objects = v;

		if (getSize() > 0) {
			selectedObject = getElementAt(0);
		}
	}

	// implements javax.swing.ComboBoxModel
	/**
	 * Set the value of the selected item. The selected item may be null.
	 * <p>
	 * 
	 * @param anObject
	 *            The combo box value or null for no selection.
	 */
	public void setSelectedItem(Object anObject) {
		if ((selectedObject != null && !selectedObject.equals(anObject)) || selectedObject == null && anObject != null) {
			selectedObject = anObject;
			fireContentsChanged(this, -1, -1);
		}
	}

	// implements javax.swing.ComboBoxModel
	public Object getSelectedItem() {
		return selectedObject;
	}

	// implements javax.swing.ListModel
	public int getSize() {
		return objects.size();
	}

	// implements javax.swing.ListModel
	public Object getElementAt(int index) {
		if (index >= 0 && index < objects.size())
			return objects.get(index);
		else
			return null;
	}

	/**
	 * Returns the index-position of the specified object in the list.
	 * 
	 * @param anObject
	 * @return an int representing the index position, where 0 is the first
	 *         position
	 */
	public int getIndexOf(Object anObject) {
		int res = objects.indexOf(anObject);
		return res;
	}

	// implements javax.swing.MutableComboBoxModel
	@SuppressWarnings("unchecked")
	public void addElement(Object anObject) {
		objects.add((E) anObject);
		fireIntervalAdded(this, objects.size() - 1, objects.size() - 1);
		if (objects.size() == 1 && selectedObject == null && anObject != null) {
			setSelectedItem(anObject);
		}
	}

	// implements javax.swing.MutableComboBoxModel
	@SuppressWarnings("unchecked")
	public void insertElementAt(Object anObject, int index) {
		index -= 1;
		objects.add(index, (E) anObject);
		fireIntervalAdded(this, index, index);
	}

	// implements javax.swing.MutableComboBoxModel
	public void removeElementAt(int index) {
		if (getElementAt(index) == selectedObject) {
			if (index == 0) {
				setSelectedItem(getSize() == 1 ? null : getElementAt(index + 1));
			} else {
				setSelectedItem(getElementAt(index - 1));
			}
		}

		objects.remove(index);

		fireIntervalRemoved(this, index, index);
	}

	    // implements javax.swing.MutableComboBoxModel
	public void removeElement(Object anObject) {
		int index = objects.indexOf(anObject);
		if (index != -1) {
			removeElementAt(index);
		}
	}

	    /**
	 * Empties the list.
	 */
	public void removeAllElements() {
		if (objects.size() > 0) {
			int firstIndex = 0;
			int lastIndex = objects.size() - 1;
			objects.clear();
			selectedObject = null;
			fireIntervalRemoved(this, firstIndex, lastIndex);
		} else {
			selectedObject = null;
		}
	}
	}
